package cn.cqie.frame.controller;


import cn.cqie.frame.bo.add.AddUserReqBo;
import cn.cqie.frame.bo.query.QueryUserListReqBo;
import cn.cqie.frame.bo.update.UpdateUserChangePsBo;
import cn.cqie.frame.bo.update.UpdateUserPsBo;
import cn.cqie.frame.bo.update.UpdateUserReqBo;
import cn.cqie.frame.bo.update.UpdateUserStatusReqBo;
import cn.cqie.frame.dto.UserContext;
import cn.cqie.frame.service.SysUserService;
import cn.cqie.frame.utils.MyPageHelper;
import cn.cqie.frame.vo.ResultVo;
import cn.cqie.frame.vo.UserVo;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHL
 * @since 2021-09-14
 */
@RestController
@Api("用户管理")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 获取登录用户信息
     * @return
     */
    @ApiOperation("获取登录用户信息")
    @GetMapping(value = "/users/info")
    public ResultVo<UserVo> getUserInfo() {
        UserVo userInfo = sysUserService.getInfoById(UserContext.get().getId());
        return ResultVo.success(userInfo);
    }

    /**
     * 查询用户信息分页
     * @param bo
     * @return
     */
    @ApiOperation("查询用户信息分页")
    @GetMapping(value = "/users/userInfoPage")
    public ResultVo<PageInfo<UserVo>> getPageList( QueryUserListReqBo bo) {
        return ResultVo.success(MyPageHelper.returnPage(sysUserService.qryByPaged(bo)));
    }

    /**
     * 新增用户
     * @param bo
     * @return
     */
    @ApiOperation("新增用户")
    @PostMapping(value = "/users/addUser")
    public ResultVo<Void> addUser(@RequestBody @Valid AddUserReqBo bo) {
        sysUserService.addUser(bo);
        return ResultVo.success();
    }

    /**
     * 更新用户信息
     * @param bo
     * @return
     */
    @ApiOperation("更新用户信息")
    @PutMapping(value = "/users/update")
    public ResultVo<Void> updateUser(@RequestBody UpdateUserReqBo bo) {
        Boolean aBoolean = sysUserService.updateUser(bo);
        return aBoolean ? ResultVo.success() : ResultVo.error("更新失败");
    }

    /**
     * 重置密码
     * @param bo
     * @return
     */
    @ApiOperation("重置密码")
    @PutMapping(value = "/users/resetPassword")
    public ResultVo<Void> resetPassword(@RequestBody UpdateUserPsBo bo) {
        Boolean aBoolean = sysUserService.resetPassword(bo);
        return aBoolean ? ResultVo.success() : ResultVo.error("更新失败");
    }

    /**
     * 修改密码
     * @param bo
     * @return
     */
    @ApiOperation("修改密码")
    @PutMapping(value = "/users/changePassword")
    public ResultVo<Void> changePassword(@RequestBody UpdateUserChangePsBo bo) {
        Boolean aBoolean = sysUserService.changePassword(bo);
        return aBoolean ? ResultVo.success() : ResultVo.error("更新失败");
    }

    /**
     * 用户启用停用
     * @param bo
     * @return
     */
    @ApiOperation("用户启用停用")
    @PutMapping(value = "/users/change/status")
    public ResultVo<Void> changeStatus(@RequestBody UpdateUserStatusReqBo bo) {
        Boolean aBoolean = sysUserService.changeUserStatus(bo);
        return aBoolean ? ResultVo.success() : ResultVo.error("更新失败");
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @ApiOperation("删除用户")
    @DeleteMapping("/users/deleteUser/{userId}")
    public ResultVo<Void> delete(@PathVariable Integer userId) {
        Boolean aBoolean = sysUserService.delete(userId);
        return aBoolean ? ResultVo.success() : ResultVo.error("删除失败");
    }
}

